Skip to content

[charts-pro] Handle edge case in export image#21190

Merged
bernardobelchior merged 3 commits into
mui:masterfrom
bernardobelchior:export-image-edge-case
Feb 2, 2026
Merged

[charts-pro] Handle edge case in export image#21190
bernardobelchior merged 3 commits into
mui:masterfrom
bernardobelchior:export-image-edge-case

Conversation

@bernardobelchior
Copy link
Copy Markdown
Member

When the iframe's body style is set to certain values (e.g., display: 'flex'), the body won't have an intrinsic size, causing the resulting canvas to have a height of 0px, which causes toBlob to return null.

You can reproduce this issue in our docs as follows:

Screen.Recording.2026-01-30.at.17.45.34.mov

@bernardobelchior bernardobelchior added type: bug It doesn't behave as expected. needs cherry-pick The PR should be cherry-picked to master after merge. scope: charts Changes related to the charts. v8.x labels Jan 30, 2026
@bernardobelchior bernardobelchior added the plan: Pro Impact at least one Pro user. label Jan 30, 2026
doc.body.removeChild(iframe);
throw new Error(
`MUI X Charts: Cannot export an image with zero width or height. Width: ${canvas.width}px. Height: ${canvas.height}px.\n` +
'If this happens, please open an issue at github.com/mui/mui-x with a reproduction of the problem.',
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessarily our problem, but it's hard for us to ask the user to debug it, so I've suggested they open an issue. If there's a better solution let me know

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have a "debug" mode that doesn't clear the iframe after so users can debug themselves.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can technically use onBeforeExport to do that by inserting a debugger statement in that callback. What we can do is maybe explain that in the docs?

@bernardobelchior bernardobelchior marked this pull request as ready for review January 30, 2026 16:49
@mui-bot
Copy link
Copy Markdown

mui-bot commented Jan 30, 2026

Deploy preview: https://deploy-preview-21190--material-ui-x.netlify.app/

Bundle size report

Bundle Parsed size Gzip size
@mui/x-data-grid 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-pro 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-premium 0B(0.00%) 0B(0.00%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 🔺+207B(+0.04%) 🔺+84B(+0.06%)
@mui/x-charts-premium 🔺+203B(+0.04%) 🔺+47B(+0.03%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 0B(0.00%) 0B(0.00%)
@mui/x-tree-view-pro 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against 061b03e

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 30, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing bernardobelchior:export-image-edge-case (061b03e) with master (adbcb8b)1

Summary

✅ 14 untouched benchmarks

Footnotes

  1. No successful run was found on master (9b628c6) during the generation of this report, so adbcb8b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Comment on lines +93 to +94
`MUI X Charts: Cannot export an image with zero width or height. Width: ${canvas.width}px. Height: ${canvas.height}px.\n` +
'If this happens, please open an issue at github.com/mui/mui-x with a reproduction of the problem.',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about what we could do from those issues.

Either it's a simple issue on their codebase styling they could fix
Or it's hard to fix and so they will open an issue

Suggested change
`MUI X Charts: Cannot export an image with zero width or height. Width: ${canvas.width}px. Height: ${canvas.height}px.\n` +
'If this happens, please open an issue at github.com/mui/mui-x with a reproduction of the problem.',
`MUI X Charts: Cannot export an image with zero width or height (width: ${canvas.width}px, height: ${canvas.height}px).`

Maybe adding an if(process.env.NODE_ENV !== 'production') to avoid throwing an error in production.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either it's a simple issue on their codebase styling they could fix

I don't think it'll be clear what to fix. I spent some time debugging to understand the root cause.

Or it's hard to fix and so they will open an issue

Yeah, that's why I'm suggesting they open an issue. That will probably help us understand more cases where this might be happening so we can preventively fix them from our side (e.g., by applying default styles).

Maybe I can only show the message "If this happens, please open an issue [...]" in development to avoid showing that to end users.

Maybe adding an if(process.env.NODE_ENV !== 'production') to avoid throwing an error in production.

What's your concern with an error? I think it's better to show an error if something goes wrong, otherwise people won't know. If you have something like Sentry to monitor errors, then these will be caught. If we just hide the error then developers won't know their users are facing problems in their app

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it'll be clear what to fix. I spent some time debugging to understand the root cause.

It will not be easier for us except if they manage to do a reproduction. And if they manage do do one, they basically solved their issue

What's your concern with an error?

From me you have two way to communicate about errors with devs:

  • console.error informs the devs that an error occurred but we mitigate it
  • Error() something went terrible and we can't do anything about that

I tend to prefer the first one because the second one without clean error boundaries can lead to some disturbing UI.

But not a big deal especially for pro users

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will not be easier for us except if they manage to do a reproduction. And if they manage do do one, they basically solved their issue

That is true. I don't feel too strongly about it, so I'm ok with removing that section of the message.

I tend to prefer the first one because the second one without clean error boundaries can lead to some disturbing UI.

Actually, just realized we're catching all the error in useChartProExport and logging them as an error, so no errors escalate.

@bernardobelchior bernardobelchior merged commit ce4a44a into mui:master Feb 2, 2026
23 checks passed
@bernardobelchior bernardobelchior deleted the export-image-edge-case branch February 2, 2026 12:31
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 2, 2026

Cherry-pick PRs will be created targeting branches: v8.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs cherry-pick The PR should be cherry-picked to master after merge. plan: Pro Impact at least one Pro user. scope: charts Changes related to the charts. type: bug It doesn't behave as expected. v8.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants